package com.gmail.shazi199.satoriex.entity.impl;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.lwjgl.input.Keyboard;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

import com.gmail.shazi199.satoriex.App;
import com.gmail.shazi199.satoriex.entity.Collision;
import com.gmail.shazi199.satoriex.utils.Coordinate;
import com.gmail.shazi199.satoriex.utils.Drawer;

public class Reimu extends Player {

    private static final Log LOGGER = LogFactory.getLog(Reimu.class);

    private Texture texture;

    private double posX = Coordinate.MAX_STAGE_WIDTH / 2;
    private double posY = Coordinate.MAX_STAGE_HEIGHT / 3 * 2;

    private boolean slow = false;

    private int moveDirection = 0;
    private int frame = 0;

    private int gameTime = 0;

    public Reimu() {
        try {
            texture = TextureLoader.getTexture("PNG", App.class.getResourceAsStream("resource/image/reimu.png"));
        } catch (IOException e) {
            LOGGER.error("Load Texture error.", e);
            App.getApp().exit();
        }
    }

    @Override
    public void draw() {
        switch (moveDirection) {
        case 0:
            Drawer.draw(texture, (int) Coordinate.getDrawX(posX) - 16, (int) Coordinate.getDrawY(posY) - 24, 32, 48, frame * 32, 0);
            break;
        case -1:
            Drawer.draw(texture, (int) Coordinate.getDrawX(posX) - 16, (int) Coordinate.getDrawY(posY) - 24, 32, 48, frame * 32, 48);
            break;
        case 1:
            Drawer.draw(texture, (int) Coordinate.getDrawX(posX) - 16, (int) Coordinate.getDrawY(posY) - 24, 32, 48, frame * 32, 96);
            break;
        default:
            break;
        }
    }

    @Override
    public double getX() {
        return posX;
    }

    @Override
    public double getY() {
        return posY;
    }

    @Override
    public double getRadius() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void getHurt(Collision other) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyboardUpdate() {
        throw new UnsupportedOperationException();
    }

    @Override
    public void update() {
        int newMoveDirection = 0;
        slow = false;
        if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
            slow = true;
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
            posY -= 4 / (slow ? 2 : 1);
            if (posY < 0) {
                posY = 0;
            }
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
            posY += 4 / (slow ? 2 : 1);
            if (posY > Coordinate.MAX_STAGE_HEIGHT) {
                posY = Coordinate.MAX_STAGE_HEIGHT;
            }
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
            posX -= 4 / (slow ? 2 : 1);
            if (posX < 0) {
                posX = 0;
            }
            newMoveDirection -= 1;
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
            posX += 4 / (slow ? 2 : 1);
            if (posX > Coordinate.MAX_STAGE_WIDTH) {
                posX = Coordinate.MAX_STAGE_WIDTH;
            }
            newMoveDirection += 1;
        }
        if (newMoveDirection != this.moveDirection) {
            this.frame = 0;
            this.moveDirection = newMoveDirection;
        }
        gameTime = (gameTime + 1) % 60;
        if (this.moveDirection == 0) {
            if (gameTime % 4 == 1) {
                frame = (frame + 1) % 8;
            }
        } else {
            if (gameTime % 4 == 1) {
                frame = frame < 7 ? frame + 1 : 4;
            }
        }
    }

}
